package net.caixiaomi.info.widgets;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by mengxianzheng on 2017/12/7.
 */

public class FingerPrinterScanView extends View {

    private Paint mBitPaint;
    private Bitmap mFingerGreen, mFingerGrey, mLine;
    private Rect mSrcRect, mDestRect;
    private int mBitWidth, mBitHeight;
    private int mWidth, mHeight;
    private float mFraction = 0f;
    private boolean isAnim = true;
    private Context mContext;

    public FingerPrinterScanView(Context context) {
        this(context, null);
    }

    public FingerPrinterScanView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FingerPrinterScanView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initBitmap();
        initPaint();
    }

    private void initPaint() {
        mBitPaint = new Paint();
        // 防抖动
        mBitPaint.setDither(true);
        // 开启图像过滤
        mBitPaint.setFilterBitmap(true);
        mSrcRect = new Rect();
        mDestRect = new Rect();
    }

    private void initBitmap() {
        mFingerGreen = ((BitmapDrawable) ContextCompat.getDrawable(mContext, net.caixiaomi.info.R.mipmap.finger_green)).getBitmap();
        mFingerGrey = ((BitmapDrawable) ContextCompat.getDrawable(mContext, net.caixiaomi.info.R.mipmap.finger_grey)).getBitmap();
        mLine = ((BitmapDrawable) ContextCompat.getDrawable(mContext, net.caixiaomi.info.R.mipmap.finger_line)).getBitmap();

        mBitWidth = mFingerGreen.getWidth();
        mBitHeight = mFingerGreen.getHeight();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
        Log.d("onSizeChanged", "width = " + w + " height = " + h);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int halfViewWidth = mWidth / 2;
        int halfViewHeight = mHeight / 2;
        int left = 0;
        int top = 0;
        mBitPaint.setAlpha(255);
        mSrcRect.set(left, top, mBitWidth, mBitHeight);
        left = halfViewWidth - mFingerGrey.getWidth() / 2;
        top = halfViewHeight - mFingerGrey.getHeight() / 2;
        mDestRect.set(left, top, mBitWidth + left, mBitHeight + top);
        canvas.drawBitmap(mFingerGrey, mSrcRect, mDestRect, mBitPaint);//画灰色底图

        left = 0;
        top = 0;
        mSrcRect.set(left, top, mBitWidth, (int) (mBitHeight * mFraction));
        left = halfViewWidth - mFingerGreen.getWidth() / 2;
        top = halfViewHeight - mFingerGreen.getHeight() / 2;

        mDestRect.set(left, top, mBitWidth + left, (int) (mBitHeight * mFraction) + top);
        canvas.drawBitmap(mFingerGreen, mSrcRect, mDestRect, mBitPaint);//画绿色图片

        left = halfViewWidth - mLine.getWidth() / 2;
        canvas.drawBitmap(mLine, left, mDestRect.bottom, mBitPaint);//画线
    }


    public void startScaning() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.f, 100.f);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @SuppressLint("NewApi")
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mFraction = valueAnimator.getAnimatedFraction();
                invalidate();
            }
        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                mFraction = 0;
                startReset();

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        if (!valueAnimator.isRunning()) {
            valueAnimator.start();
        }
    }

    private void startReset() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0.f, 100.f);
        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mFraction = 1-valueAnimator.getAnimatedFraction();
                invalidate();
            }
        });
        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                startScaning();
            }
        });
        if (!valueAnimator.isRunning()) {
            valueAnimator.start();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec));
    }

    private int measureWidth(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = mLine.getWidth();
        if (specMode == MeasureSpec.AT_MOST) {//相当于我们设置为wrap_content
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {//相当于我们设置为match_parent或者为一个具体的值
            result = specSize;
        }
        return result;
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = mFingerGrey.getHeight() + getPaddingBottom() + getPaddingTop();

        if (specMode == MeasureSpec.AT_MOST) {
            result = specSize;
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }


}
